package com.dao;

import com.entity.ConfigEntity;
import com.entity.ConfigFileEntity;
import com.ex.MyException;
import top.appx.easysql.BaseDatabase;
import top.appx.easysql.Restrain;
import top.appx.espring.annotation.Source;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;


@Source
public class ConfigFileDao extends BaseDao<ConfigFileEntity> {
    @Override
    public void add(ConfigFileEntity configFileEntity){
        BaseDatabase db = getDatabase();
        try{
            db.beginTrans();
            db.add(configFileEntity);
            if(db.total(entityClass, Restrain.eq("filename",configFileEntity.getFilename()))>1){
                throw new MyException("文件名不能重复");
            }
            FileOutputStream fos = new FileOutputStream(configFileEntity.getFilename());
            fos.write(configFileEntity.getValue().getBytes());
            fos.close();
            db.commitTrans();
        }catch (Exception ex){
            db.rollback();
            throw new MyException(ex.getMessage());
        }finally {
            db.close();
        }
    }

    @Override
    public void modify(ConfigFileEntity configFileEntity){
        BaseDatabase db = getDatabase();
        try{
            db.beginTrans();
            ConfigFileEntity old_configFileEntity = db.query(entityClass,Restrain.eq("id",configFileEntity.getId())).get(0);
            db.modify(configFileEntity);
            if(db.total(entityClass, Restrain.eq("filename",configFileEntity.getFilename()))>1){
                throw new MyException("文件名不能重复");
            }
            FileOutputStream fos = new FileOutputStream(configFileEntity.getFilename());
            fos.write(configFileEntity.getValue().getBytes());
            fos.close();

            if(!old_configFileEntity.getFilename().equals(configFileEntity.getFilename())){
                File f = new File(old_configFileEntity.getFilename());
                if(f.exists()){
                    boolean r = f.delete();
                    if(!r){
                        throw new MyException("删除旧文件失败");
                    }
                }
            }



            db.commitTrans();
        }catch (Exception ex){
            db.rollback();
            throw (RuntimeException)ex;
        }finally {
            db.close();
        }
    }
    @Override
    public void delById(int id){
        BaseDatabase db = getDatabase();
        try{
            db.beginTrans();
            List<ConfigFileEntity> configFileEntitys = db.query(entityClass,Restrain.eq("id",id));
            if(configFileEntitys.size()==0){
                return;
            }
            ConfigFileEntity old_configFile = configFileEntitys.get(0);
            db.delById(entityClass,id);

            File f = new File(old_configFile.getFilename());
            if(f.exists()){
                boolean r = f.delete();
                if(!r){
                   throw new MyException("删除文件失败");
                }
            }
            db.commitTrans();
        }catch (Exception ex){
            db.rollback();
            throw new MyException(ex.getMessage());
        }finally {
            db.close();
        }
    }
}
